// Copyright (c) 2014 Andrey Menangen

#include "Python.h"
#include <stdio.h>
#include <stdbool.h>

#ifdef _MSC_VER
#define inline __inline
#endif


static PyObject *
py_get_number(PyObject *self, PyObject *args) {

	int count1, count2, n, n2;
	char *array_of_bytes;
	unsigned char item;

	PyObject *PyLIST, *bytearray;
	

	if (
		!PyArg_ParseTuple
		(
			args, "O", &PyLIST
		)

		)
		return NULL;
	
	n = PyList_Size(PyLIST);

	for (count1 = 0; count1 < n; count1++) {

        bytearray = PyList_GetItem(PyLIST, count1);
        n2 = (int)PyByteArray_Size(bytearray);
        //printf("%d\n", n2);

        array_of_bytes = PyByteArray_AsString(bytearray);

        for (count2 = 0; count2 < n2; count2++) {
        	item = array_of_bytes[count2];
			printf("A python Object iter %d\n", item);
		
		};
       

		printf("\n");	
    };
	
	return Py_BuildValue("[i,i,i,i]", 12, 11, 10, 12);
};


static PyObject *
get_around_pixels(PyObject *self, PyObject *args) {
	const bool debuging = false;

	int x, y;
	unsigned int pixel_x, pixel_y;	
	unsigned int offset, w_square, s_pixels, center_pixel_number;	
	
	int area_width, area_height, MATRIX[2];
	MATRIX[0] = 400; MATRIX[1] = 250;

	Py_ssize_t lenght;
	PyObject *Py_main_pixel_on_map, *Py_around_step;
	PyObject *attr_name, *attribute_value;
	PyObject *result_pixels_list;
	PyObject *inside_pixel_list;


	if (
		!PyArg_ParseTuple
		(
			args, "OO", &Py_main_pixel_on_map, &Py_around_step
		)

		)
		return NULL;
	offset = (int)PyInt_AsLong(Py_around_step);

	attr_name = PyString_FromString("x");
	attribute_value = PyObject_GetAttr(Py_main_pixel_on_map, attr_name);

	pixel_x = (int)PyInt_AsLong(attribute_value);


	attr_name = PyString_FromString("y");
	attribute_value = PyObject_GetAttr(Py_main_pixel_on_map, attr_name);

	pixel_y = (int)PyInt_AsLong(attribute_value);


	lenght = 0;
	result_pixels_list = PyList_New(lenght);

	area_width = MATRIX[0];
    area_height = MATRIX[1];

    w_square = (offset * 2) + 1;
    s_pixels = w_square * w_square;

    center_pixel_number = (s_pixels - 1) / 2;

    for (int pixel_id_in_square = 0; pixel_id_in_square < s_pixels; pixel_id_in_square++) {

        x = (pixel_id_in_square % w_square) - (offset - pixel_x);
        y = (pixel_id_in_square / w_square) - (offset - pixel_y);
        
        if (debuging) { printf("x = %i\n", x); printf("y = %i\n", y); }//printf("\n");       

        // Pixel in area?
        if ( (area_width > x && x > -1) && (area_height > y && y > -1) ) {
            
            if (pixel_id_in_square != center_pixel_number) {
                if (debuging) {
                    printf("Add square id: %i\n", pixel_id_in_square);
                    //printf("Insert: x=%i, y=%i\n", x, y);
                }                
                
                inside_pixel_list = Py_BuildValue("[i,i]", x, y);
                PyList_Append(result_pixels_list, inside_pixel_list);
            };
        };
    };

    if (debuging) {
        printf("w_square: [%i], s_pixels: [%i], center_pixel_number: [%i]\n", w_square, s_pixels, center_pixel_number);
        //printf("x=%i, y=%i\n", x, y);
    }   

	return result_pixels_list;
};


static PyMethodDef functions[] = {
	{"getnumber", (PyCFunction) py_get_number, METH_VARARGS | METH_KEYWORDS, 
		"py_getnumber"
		"Getting Number"},
	{"get_around_pixels", (PyCFunction) get_around_pixels, METH_VARARGS | METH_KEYWORDS, 
		"get_around_pixels"
		"Getting Pixels around center pixel in relative coordinates"},
	{NULL}
};

void
init_noisemethod(void)
{
	Py_InitModule3("_noisemethod", functions, 
		"Methods for iterate throught perlin noise List[]");
};